<template>
  <el-form :model="form" ref="ruleFormRef" :rules="rules" status-icon :label-position="labelPosition" label-width="120px" :inline="true" class="demo-form-inline">
    <el-form-item label="所属单位" prop="unit">
      <!-- <SelectOption :selectValue="form.unit" :options="data.orgAllList" @change-value="changeValue2"> </SelectOption> -->

      <popup-tree-input :data="orgAllList" :props="data.popupTreeProps" :prop="deptForm.parentName == null
          ? $t('message.org.top_menu')
          : form.unit
        " :node-key="'' + deptForm.parentId" :current-change-handle="changeValue2" />
    </el-form-item>
    <el-form-item label="姓名" prop="userName">
      <!-- <SelectOption :selectValue="form.userName" :options="data.userAllList" @change-value="changeValue"> </SelectOption> -->
      <user-list-modal
        v-model="form.userName"
        @update:modelValue="changeValue"
        :default-users="[]"
        :multiple="false"
        :orgid="data.userOrgId" 
        :noOrgTip="!form.unitId"
        :devType="'platform'"
      />
    </el-form-item>
    <!-- <el-form-item label="性别" prop="sex">
      <SelectOption :selectValue="form.sex" :options="data.sexList" @change-value="changeValue1"> </SelectOption>
    </el-form-item> -->

    <!-- <el-form-item label="年龄" prop="birthday">
      <el-input v-model="form.birthday" />
    </el-form-item> -->
    <el-form-item label="交流类别" prop="communicateClass">
      <SelectOption :selectValue="form.communicateClass" :options="data.gjhLevelSelect" @change-value="changeValue3"> </SelectOption>
    </el-form-item>
    <el-form-item label="交流方式" prop="communicateStyle">
      <SelectOption :selectValue="form.communicateStyle" :options="data.cooperateList" @change-value="changeValue4"> </SelectOption>
    </el-form-item>
    <el-form-item label="交流时间" prop="communicateDate">
      <el-date-picker v-model="form.communicateDate" format="YYYY-MM-DD" type="date" placeholder="选择日期" clearable @change="handleChangeDate" />
    </el-form-item>

    <el-form-item style="width: 100%" label="详细信息" prop="detailInfo">
      <el-input v-model="form.detailInfo" :autosize="{ minRows: 2 }" type="textarea" placeholder="请输入" maxlength="200" show-word-limit />
    </el-form-item>
    <el-form-item label="备注" style="width: 100%">
      <el-input v-model="form.remark" :autosize="{ minRows: 2 }" maxlength="200" show-word-limit type="textarea" placeholder="请输入" />
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import UserListModal from '@/views/project/components/UserListModal/index.vue';
import PopupTreeInput from '@/views/science/components/PopupTreeInput/index.vue';
import SelectOption from '@/views/science/components/selectOption/index.vue';
import { computed, reactive, ref, watch } from 'vue';
import { useStore } from 'vuex';


export default {
  components: {
    SelectOption,UserListModal,PopupTreeInput
  },

  props:{
    orgAllList: {
      type: Array,
      required: true,
    },
  },
  emits: {
    valueFlag: null,
  },
  setup(props, { emit }) {

    const deptForm = {
            parentId: 0,
            parentName: '',
        }

    const store = useStore()
    const labelPosition = ref('right')
    const form = reactive(store.state.technologyStrength.talentInfo)

    const data = reactive({
        popupTreeProps: {
                  label: 'name',
                  children: 'children',
              },
        addUnit: false,
        innerValue: '',
        outsideValue: '',

        userAllList: computed(() => store.state.technologyStrength.userAllList),
        sexList: computed(() => store.state.technologyStrength.sexList),
        gjhLevelSelect: computed(() => store.state.technologyStrength.gjhLevelSelect),
        cooperateList: [],
        userOrgId:''
    })
    watch(()=>form.unitId,()=>{
      data.userOrgId =  form.unitId
    })

    if(form.communicateClassId){
      data.gjhLevelSelect.map((item)=>{
        if(item.itemValue == form.communicateClassId){
          data.gjhLevelSelect = item.children
        }
      })
    }
    watch(form, (newValue, oldValue) => {
      console.log("form值改变了")
      store.commit('technologyStrength/gettalentInfo', { ...form })
    });

    const changeValue = (item) => {
      form.userName = item.cname
      form.userId = item.userId
      // form.sex = item.gendertext
      // form.birthday = item.age
    }

    const changeValue1 = (item) => {
      form.sex = item.itemText
      // form.buildUnitId = item.value
    }
    const changeValue2 = (item) => {
      console.log('changeValue7');  
      data.userOrgId = item.id;
      form.unit = item.name
      form.unitId = item.id
    }
    const changeValue3 = (item) => {
      form.communicateClass = item.itemText
      form.communicateClassId = item.itemValue
      data.cooperateList = item.children
      form.communicateStyle = ''
      form.communicateStyleId = ''
    }
    const changeValue4 = (item) => {
      form.communicateStyle = item.itemText
      form.communicateStyleId = item.itemValue
    }

    const rules = reactive({
      userName: [
        { required: true, message: '请选择姓名', trigger: 'change' },
      ],
      sex: [
        { required: true, message: '请选择性别', trigger: 'change', },
      ],
      unit: [
        { required: true, message: '请选择所属单位', trigger: 'change', },
      ],
      birthday:[
        { required: true, message: '请输入年龄', trigger: 'blur', },
      ],
      communicateClass: [
        { required: true, message: '请选择交流类别', trigger: 'change', },
      ],
      communicateStyle: [
        { required: true, message: '请选择交流方式', trigger: 'change', },
      ],
      communicateDate: [
        { required: true, message: '请选择交流时间', trigger: 'change' },
      ],
      detailInfo: [
        { required: true, message: '请输入详细信息', trigger: 'blur' },
      ],
    })

    const ruleFormRef = ref()
    const submitForm = async () => {
      await ruleFormRef.value.validate((valid, fields) => {
        if (valid) {
          emit('valueFlag', true)
          console.log('submit!')
        } else {
          emit('valueFlag', false)
        }
      })
    }

    const handleChangeDate = (val) => {
      const year = val.getFullYear(); // 获取年份
      const month = (val.getMonth() + 1) < 10 ? '0' + (val.getMonth() + 1) : (val.getMonth() + 1); // 获取月份，需要加1
      const day = val.getDate() < 10 ? '0' + val.getDate() : val.getDate(); // 获取日期
      const formattedDate = `${year}-${month}-${day}`;
      form.communicateDate = formattedDate
      store.commit('technologyStrength/gettalentInfo', { ...form })
    }


    return {
      labelPosition,
      data,
      form,
      rules,
      ruleFormRef,
      submitForm,
      handleChangeDate,
      deptForm,
      changeValue,
      changeValue1,
      changeValue2,
      changeValue3,
      changeValue4
    }
  },
}
</script>
<style lang="scss" scoped>
.demo-form-inline .el-form-item {
  width: 48%;
}

:deep(.labelHeight .el-form-item__label) {
  line-height: 20px;
}

:deep(.el-date-editor.el-input) {
  width: 100%;
}

:deep(.el-date-editor.el-input__inner) {
  width: 100%;
}

:deep(.demo-form-inline .el-select) {
  width: 100%;
  margin: 0;
}

.el-select {
  width: 100%;
  margin: 0;
}

.selectLabel {
  width: 70%;
  min-height: 40px;
  line-height: 40px;
  padding: 0 15px;
  border: 1px solid #ccc;
  border-radius: 6px;
  display: inline-block;
}

.addLabel {
  display: inline-block;
  width: 120px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  background-color: #169bd5;
  border-radius: 6px;
  color: #fff;
  margin-left: 15px;
}

.unitBox {
  width: 100%;
  margin: 30px 0;

  .title {
    padding: 15px 0;
    border-bottom: 1px solid #ccc;
  }

  .tagBox {
    line-height: 40px;
    margin-top: 15px;
    border: 1px solid #ccc;
    border-radius: 8px;
    padding: 10px 15px;
  }
}

.el-tag {
  margin-right: 10px;
}
</style>
